#!/bin/sh
#
# start-all -- start all stopped docker lab
#
# Copyright (C) 2016-2021 Wu Zhangjin <falcon@ruma.tech>
#

export RELEASE=0

TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd)"
. "$TOP_DIR"/tools/docker/config >/dev/null

log_print "Check exited containers"
id_names=`docker ps -f status=exited --format='{{.ID}},{{.Names}}' -q`

[ -z "$id_names" ] && info_print "No exited container found."  && exit 0

for id_name in $id_names
do
    id=$(echo $id_name | cut -d ',' -f1)
    name=$(echo $id_name | cut -d ',' -f2)

    if [ "$name" = "$WPROXY_NAME" -o "$name" = "$TPROXY_NAME" ]; then
      info_print "Start $name ..."
      docker start $name
      continue
    fi

    lab=`echo $name | sed -e "s%-[0-9]*-[0-9a-zA-Z]\{6,6\}$%%g"`
    get_labs | tr ' ' '\n' | grep -q ^$lab$
    [ $? -ne 0 ] && continue

    log_print "Start $lab ..."
    "$DOCKER_START_CMD" $lab
done

log_print "Update release page"
"$DOCKER_RELEASE_CMD" all >/dev/null 2>&1
